/*
 * Copyright 2009-2011 Cedric Priscal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>
#include "HighPressure.h"
//#include <cutils/log.h>
#include "android/log.h"
static const char *TAG="high_pressure";
//#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO,  TAG, fmt, ##args)
//#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
//#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
#define  LOG_TAG    "high_pressure"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define HIGH_PRESSURE_DEV "/dev/high_pressure_dev"
#define HIGH_PRESSURE_POWER_ON            _IOWR('M',0x01,int)
#define HIGH_PRESSURE_POWER_OFF           _IOWR('M',0x02,int)

#define HIGH_OTG_POWER_ON                 _IOWR('M',0x03,int)
#define HIGH_OTG_POWER_OFF                _IOWR('M',0x04,int)




JNIEXPORT jint JNICALL Java_com_smartmate_electricshock_HighPressure_poweron (JNIEnv *env, jobject thiz)
{
	int fd;
	fd = open(HIGH_PRESSURE_DEV, O_RDWR);
	if (fd == -1)
	{
		LOGI("highpressure-poweron: fd is : %d ", fd);
	}
	if(ioctl(fd, HIGH_PRESSURE_POWER_ON, 0) < 0) {
      //  close(fd);
	}
	LOGI("highpressure-poweron: fd is : %d ", fd);
	   close(fd);
	   return fd;
}
JNIEXPORT jint JNICALL Java_com_smartmate_electricshock_HighPressure_poweroff (JNIEnv *env, jobject thiz)
{
	int fd;
	fd = open(HIGH_PRESSURE_DEV, O_RDWR);
	if (fd == -1)
	{
		LOGI("highpressure-poweroff: fd is : %d ", fd);
	}

	if(ioctl(fd, HIGH_PRESSURE_POWER_OFF, 0) < 0) {
      //  close(fd);
	}
	LOGI("highpressure-poweroff: fd is : %d ", fd);
	   close(fd);
	   return fd;
}
JNIEXPORT jint JNICALL Java_com_smartmate_electricshock_HighPressure_readbuf (JNIEnv *env, jobject thiz)
{
	int fd;
	int buf;
	fd = open(HIGH_PRESSURE_DEV, O_RDWR);
	read(fd,&buf,4);

	return buf;
}

JNIEXPORT jint JNICALL Java_com_smartmate_electricshock_HighPressure_otgopen (JNIEnv *env, jobject thiz)
{
	int fd;
	fd = open(HIGH_PRESSURE_DEV, O_RDWR);
	if (fd == -1)
	{
		LOGI("highpressure-poweron: fd is : %d ", fd);
	}
	if(ioctl(fd, HIGH_OTG_POWER_ON, 0) < 0) {
		//  close(fd);
	}
	LOGI("highpressure-poweron: fd is : %d ", fd);
	close(fd);
	return fd;
}
JNIEXPORT jint JNICALL Java_com_smartmate_electricshock_HighPressure_otgclose (JNIEnv *env, jobject thiz)
{
	int fd;
	fd = open(HIGH_PRESSURE_DEV, O_RDWR);
	if (fd == -1)
	{
		LOGI("highpressure-poweron: fd is : %d ", fd);
	}
	if(ioctl(fd, HIGH_OTG_POWER_OFF, 0) < 0) {
		//  close(fd);
	}
	LOGI("highpressure-poweron: fd is : %d ", fd);
	close(fd);
	return fd;
}


